package world;

import java.util.ArrayList;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

public class EventStore {

		ArrayList<Event> events = new ArrayList<Event>();
		ArrayList<Path> paths = new ArrayList<Path>();
		Image town;	
	
	public EventStore() throws SlickException{
		
		town = new Image("res/world/town.png");

		// Event locations
		events.add(new Event("FIRST",  1400, 610, town));
		events.add(new Event("SECOND",  1590, 700, town));
		events.add(new Event("THIRD",  1600, 400, town));
		events.add(new Event("FOURTH",  1503, 513, town));
		events.add(new Event("FIFTH",  1706, 559, town));
		events.add(new Event("SIXTH",  1701, 403, town));
		
		events.add(new Event("SEVENTH",  1829, 503, town));
	
		events.add(new Event("EIGHTH",  1763, 660, town));
		events.add(new Event("NINTH",  1706, 696, town));
		
		//events.get(3).setStop(true);;
		

		//add paths between locations.
		paths.add(new Path(events.get(0), events.get(1)));
		paths.add(new Path(events.get(1), events.get(2)));
		paths.add(new Path(events.get(2), events.get(3)));
		paths.add(new Path(events.get(3), events.get(4)));
		paths.add(new Path(events.get(4), events.get(5)));
		paths.add(new Path(events.get(4), events.get(6)));
		paths.add(new Path(events.get(4), events.get(7)));
		paths.add(new Path(events.get(1), events.get(8)));
		
		
		for(int i = 0; i < events.size(); i++){events.get(i).setVisible(true);}
		
		
		for(int j = 0; j < paths.size(); j++){paths.get(j).setActive(true);}
		

		
	}
	
	public void renderEventLocations(Graphics g, float tokenPositionX, float tokenPositionY){
		
		g.setColor(Color.white);
		
		for(int i = 0; i < events.size(); i++){

			float x = events.get(i).getX();
			float y = events.get(i).getY();
			boolean checkVisible = events.get(i).isVisible();
			
				if(checkVisible){
					
					int height = events.get(i).getImage().getHeight();
					int width = events.get(i).getImage().getWidth();
		
				events.get(i).getImage().draw(tokenPositionX+ x - (width/2), tokenPositionY+y - (height/2));
				
					for(int j = 0; j < events.get(i).getPaths().size(); j++){
	
					float oppositeX = events.get(i).getPaths().get(j).getOpposite(events.get(i)).getX();
					float oppositeY = events.get(i).getPaths().get(j).getOpposite(events.get(i)).getY();
					
						if(events.get(i).getPaths().get(j).isActive()){
						g.drawLine(tokenPositionX + x, tokenPositionY+y, tokenPositionX + oppositeX, tokenPositionY + oppositeY);	
						}
					}
				}
		}		
	}	
	
	public void addPath(Event event1, Event event2){paths.add(new Path(event1, event2));}

	public boolean activatePath(Event event1, Event event2){
		
		for(int i = 0; i < event1.getPathCount(); i++){
			if(event1.getPaths().get(i).getOpposite(event1) == event2){event1.getPaths().get(i).setActive(true); return true;}
		}
		return false;
	}
	
	public boolean activatePath(Path path){
		
		for(int i = 0; i < paths.size(); i++){
			if(path == paths.get(i)){path.setActive(true); return true;}
		}
		
		return false;
	}

public boolean deactivatePath(Path path){
		
		for(int i = 0; i < paths.size(); i++){
			if(path == paths.get(i)){path.setActive(false); return true;}
		}
		
		return false;
	}

	public ArrayList<Event> getEvents(){return events;}
	public ArrayList<Path> getPaths(){return paths;}
}
